package com.findu.activity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.example.findu.R;
import com.example.findu.R.id;
import com.example.findu.R.layout;
import com.example.findu.R.menu;
import com.findu.control.ExpressionAdapter;
import com.findu.control.ExpressionPagerAdapter;
import com.findu.control.Public_title;
import com.findu.util.SmileUtils;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Build;

public class ChatActivity extends Activity {

	private List<String> reslist;
	Public_title public_title;
	private View include_view;
	private ViewPager expressionViewpager;
	private EditText mEditTextContent;
	private View more;
	private Button btnMore;
	private ImageView iv_emoticons_normal;
	private ImageView iv_emoticons_checked;
	private LinearLayout emojiIconContainer;
	private LinearLayout btnContainer;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.activity_chat);
		include_view = findViewById(R.id.fragment_conversation_page1_include);
		public_title = new Public_title(include_view);	// 新建工具类设置布局控件	
		public_title.setRighttImage(R.drawable.ic_launcher);
		public_title.setTextView("聊天");
		
		expressionViewpager = (ViewPager) findViewById(R.id.vPager);
		mEditTextContent = (EditText) findViewById(R.id.et_sendmessage);
		more = findViewById(R.id.more);
		iv_emoticons_normal = (ImageView) findViewById(R.id.iv_emoticons_normal);
		iv_emoticons_checked = (ImageView) findViewById(R.id.iv_emoticons_checked);
		btnMore = (Button) findViewById(R.id.btn_more);
		emojiIconContainer = (LinearLayout) findViewById(R.id.ll_face_container);
		btnContainer = (LinearLayout) findViewById(R.id.ll_btn_container);
		
		// 初始化表情viewpager
		reslist = getExpressionRes(35);
		List<View> views = new ArrayList<View>();
		View gv1 = getGridChildView(1);
		View gv2 = getGridChildView(2);
		views.add(gv1);
		views.add(gv2);
		expressionViewpager.setAdapter(new ExpressionPagerAdapter(views));
		
		mEditTextContent.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				more.setVisibility(View.GONE);
				iv_emoticons_normal.setVisibility(View.VISIBLE);
				iv_emoticons_checked.setVisibility(View.GONE);
			}
			
		});
		
		iv_emoticons_normal.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				more.setVisibility(View.VISIBLE);
				iv_emoticons_normal.setVisibility(View.GONE);
				iv_emoticons_checked.setVisibility(View.VISIBLE);
				btnMore.setVisibility(View.GONE);
				emojiIconContainer.setVisibility(View.VISIBLE);
				btnContainer.setVisibility(View.GONE);
			}
		});
		
		iv_emoticons_checked.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				more.setVisibility(View.GONE);
				iv_emoticons_normal.setVisibility(View.VISIBLE);
				iv_emoticons_checked.setVisibility(View.GONE);
				btnMore.setVisibility(View.VISIBLE);
			}
		});
		
		btnMore.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				more.setVisibility(View.VISIBLE);
				iv_emoticons_normal.setVisibility(View.VISIBLE);
				iv_emoticons_checked.setVisibility(View.GONE);
				emojiIconContainer.setVisibility(View.GONE);
				btnContainer.setVisibility(View.VISIBLE);				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.chat, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public List<String> getExpressionRes(int getSum) {
		List<String> reslist = new ArrayList<String>();
		for (int x = 1; x <= getSum; x++) {
			String filename = "ee_" + x;

			reslist.add(filename);

		}
		return reslist;

	}
	
	private View getGridChildView(int i){
		View view = View.inflate(this, R.layout.expression_gridview, null);
		GridView gv = (GridView) view.findViewById(R.id.gridview);
		List<String> list = new ArrayList<String>();
		if (i == 1) {
			List<String> list1 = reslist.subList(0, 20);
			list.addAll(list1);
		} else if (i == 2) {
			list.addAll(reslist.subList(20, reslist.size()));
		}
		list.add("delete_expression");
		final ExpressionAdapter expressionAdapter = new ExpressionAdapter(this, 1, list);
		gv.setAdapter(expressionAdapter);
		gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				String filename = expressionAdapter.getItem(position);
				if (filename != "delete_expression"){
					try {
						Field field = SmileUtils.class.getField(filename);
						if (field != null) {
							mEditTextContent
									.append(SmileUtils.getSmiledText(
											ChatActivity.this,
											(String) field.get(null)));
						}
					} catch (Exception e) {
						// TODO: handle exception
						Log.e("FindU", e.getMessage());
					}
					
				}else if(!TextUtils.isEmpty(mEditTextContent.getText())){
					int selectionStart = mEditTextContent.getSelectionStart();
					if (selectionStart > 0){
						String body = mEditTextContent.getText().toString();
						String tempStr = body.substring(0, selectionStart);
						int i = tempStr.lastIndexOf("[");
						if (i != -1) {
							CharSequence cs = tempStr.substring(i, selectionStart);
							if (SmileUtils.containsKey(cs.toString()))
								mEditTextContent.getEditableText().delete(i, selectionStart);
							else
								mEditTextContent.getEditableText().delete(selectionStart - 1,
										selectionStart);
						} else {
							mEditTextContent.getEditableText().delete(selectionStart - 1, selectionStart);
						}
					}
				}
			}
			
		});
		return view;
	}
}
